package MainBoard;

import Model.TimeThread;
import Menu.InitialMenu;
import Dialog.BackDialog;
import Dialog.ExitDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class PVEMainBoard extends MainBoard {
    //    游戏界面实例
    private PVEChessBoard chessBoard;
    //    游戏难度
    private final int level;
    //    界面按键
    protected JButton buttonStart;
    protected JButton buttonBack;
    protected JButton buttonExit;
    //    返回对话框
    private BackDialog backDialog;

    /**
     * 初始化PVEMainBoard
     *
     * <p>PVEMainBoard是MainBoard的子类
     * 该界面为棋室对战界面
     * 通过调用MainBoard的构造函数来构造PVEMainBoard的实例</p>
     *
     * @param level 玩家选择的挑战难度
     */
    public PVEMainBoard(int level) {
        super();
        this.initBoard();
        this.level = level;
    }

    /**
     * 初始化界面的样式
     *
     * <p>包括棋盘和开始游戏、悔棋、返回三个按钮</p>
     */
    public void initBoard() {
        chessBoard = new PVEChessBoard(this);
        buttonStart = new JButton("开始游戏");
        buttonBack = new JButton("悔    棋");
        buttonExit = new JButton("返    回");
        backDialog = new BackDialog(this, "返回菜单", true);

        //    设置棋盘
        chessBoard.setClickable(CAN_NOT_CLICK);
        chessBoard.setBounds(0, 30, 600, 600);
        chessBoard.setVisible(true);

        //    设置按钮
        buttonStart.setBounds(600, 400, 200, 50);
        buttonBack.setBounds(600, 460, 200, 50);
        buttonExit.setBounds(600, 520, 200, 50);
        setButtonStyle(buttonStart);
        setButtonStyle(buttonBack);
        setButtonStyle(buttonExit);

        //    添加组件
        this.add(chessBoard);
        this.add(buttonStart);
        this.add(buttonBack);
        this.add(buttonExit);
    }

    /**
     * 获取游戏难度
     *
     * @return 游戏难度
     */
    public int getLevel() {
        return level;
    }

    /**
     * 通过用户点击的按键来相应不同的事件
     *
     * <p>当用户点击开始游戏时会将游戏启动，此时开始游戏按钮不可再点击
     * 当用户点击悔棋时会退回到上一步
     * 当用户点击返回时会返回到开始菜单</p>
     *
     * @param e 用户点击的按键
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == super.getCloseButton()) {
            exitDialog.setVisible(true);
            if (exitDialog.getMessage() == ExitDialog.exit) {
                System.exit(0);
            }
        } else if (e.getSource() == super.getSmallButton()) {
            setExtendedState(ICONIFIED);
        } else if (e.getSource() == buttonStart) {
            buttonStart.setEnabled(false);
            buttonStart.setText("正在游戏");
            chessBoard.setRole();
            timeThread = new TimeThread(timeCountLabel);
            timeThread.start();
            chessBoard.setClickable(CAN_CLICK);
            chessBoard.setResult(ChessBoard.GAME_START);
        } else if (e.getSource() == buttonBack) {
            chessBoard.backStep();
        } else if (e.getSource() == buttonExit) {
            backDialog.setVisible(true);
            if (backDialog.getMessage() == BackDialog.back) {
                this.dispose();
                new InitialMenu();
            }
        }
    }
}
